package net.stogi.unicore.beyond.ui.client.taskeditor.propertyeditor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.stogi.common.mvp.core.behavior.BehaviorWithValue;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;

class PropertyViewerWidget extends Composite implements PropertyViewerView {

	private class PropertyModel extends BaseModel {

		private static final long serialVersionUID = -4881343845959588526L;

		public PropertyModel(String key, String value) {
			set("key", key);
			set("value", value);
		}

		public String getKey() {
			return get("key");
		}
	}

	private final ContentPanel mainPanel = new ContentPanel();

	private final Grid<PropertyModel> propertyGrid;

	private final ListStore<PropertyModel> propertyStore = new ListStore<PropertyModel>();

	public PropertyViewerWidget() {
		List<ColumnConfig> columnConfigList = new ArrayList<ColumnConfig>();

		columnConfigList.add(new ColumnConfig("key", "Key", 150));
		columnConfigList.add(new ColumnConfig("value", "Value", 150));

		for (ColumnConfig columnConfig : columnConfigList) {
			columnConfig.setMenuDisabled(true);
		}
		
		propertyGrid = new Grid<PropertyModel>(propertyStore, new ColumnModel(columnConfigList));

		initComposite();
		initI18n();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void clear() {
		propertyStore.removeAll();
	}

	@Override
	public void displayProperties(Map<String, String> properties) {
		propertyStore.removeAll();
		List<PropertyModel> propertiesModel = new ArrayList<PropertyModel>();
		for (Entry<String, String> entry : properties.entrySet()) {
			propertiesModel.add(new PropertyModel(entry.getKey(), entry.getValue()));
		}
		propertyStore.add(propertiesModel);
		propertyStore.setSortField("key");
		propertyStore.setSortDir(SortDir.ASC);
	}

	@Override
	public void setPropertyDoubleClicked(final BehaviorWithValue<String> behavior) {
		propertyGrid.addListener(Events.CellDoubleClick, new Listener<GridEvent<PropertyModel>>() {

			@Override
			public void handleEvent(GridEvent<PropertyModel> be) {
				PropertyModel selectedItem = be.getGrid().getSelectionModel().getSelectedItem();
				if (selectedItem != null) {
					behavior.behave(selectedItem.getKey());
				}
			}
		});
	}

	private void initComposite() {
		propertyGrid.setAutoExpandColumn("value");
		propertyGrid.setBorders(true);
//		propertyGrid.setEnableColumnResize(false);
		propertyGrid.setHeight(250);
		propertyGrid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		propertyStore.setSortField("key");
		propertyStore.setSortDir(SortDir.ASC);
		mainPanel.setFrame(true);
	}

	private void initI18n() {
		mainPanel.setHeading("Property Editor");
	}

	private void initLayout() {
		mainPanel.add(propertyGrid);
	}
}
